package org.mcs.controlador.mantenimiento;   
import java.io.IOException;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List; 

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;  
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes; 
import com.google.gson.Gson; 
import com.google.gson.GsonBuilder;

import emp.entidades.EntModulo; 
import emp.jsf.servicio.ModuloService; 
   

@Controller  
public class ModuloController {

	@Autowired
	ModuloService moduloService;
	   
	
	public static String URL_PAGINA="modulo-mnt"; 
	
	  @RequestMapping(method=RequestMethod.GET,value="/modulo")
	   public ModelAndView carga() { 
		  EntModulo modulo=new EntModulo();    
		  ModelAndView mov= new ModelAndView(URL_PAGINA, "command",modulo); 
		  return mov;
	   }
	   
	  
	  @RequestMapping(method=RequestMethod.GET, value="modulo/lista") 
	   public    void lista(  HttpServletResponse response  ) throws IOException {
		  List<EntModulo> lista=moduloService.listarModulos(); 
		  
		  for(EntModulo m:lista){
			  DateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
			  String s = df.format(m.getFechaCrea()); 
			  try {
				m.setFechaCrea(df.parse(s));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		  }
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
	  		String resultado=gson.toJson(lista);
	  		
	  		OutputStream os = response.getOutputStream();
			byte[] content =resultado.getBytes();
			os.write(content);
			response.setContentType("text/html;charset=iso-8859-1");
			response.setContentLength(content.length); 
	   }
 
 
	  
	  @RequestMapping(value = "modulo/nuevo", method = RequestMethod.POST)
	   public ModelAndView nuevo(@ModelAttribute("SpringWeb")EntModulo modulo, ModelMap model, final RedirectAttributes redirectAttributes) {
		  EntModulo moduloNuevo=new EntModulo();    
		  ModelAndView mov=new ModelAndView(URL_PAGINA, "command",moduloNuevo ); 
		  try { 
			  if(modulo!=null && modulo.getNombre()!=null){  
				  			modulo.setEstado(1);
						  EntModulo moduloGrabado=moduloService.grabarModulo(modulo);
					      if(moduloGrabado.getId()==null){ 
					    	  mov.addObject( "messageerror","Error al grabar. Vuelva a intentarlo");
					      }
			  }
			} catch (Exception e) { 
				mov.addObject(  "messageerror","Error generico. Si el problema persiste contacte a soporte"); 
				e.printStackTrace();
			}finally{ 
				return mov;
			}
	   }
	  
	  
	  @RequestMapping(value = "modulo/modifica", method = RequestMethod.POST)
	   public ModelAndView modifica(@ModelAttribute("SpringWeb")EntModulo modulo, ModelMap model, final RedirectAttributes redirectAttributes) {
		  EntModulo moduloNuevo=new EntModulo();    
		  ModelAndView mov=new ModelAndView(URL_PAGINA, "command",moduloNuevo);

		  try {
			  
			  if(modulo!=null && modulo.getNombre()!=null){ 
						  EntModulo moduloGrabado=moduloService.grabarModulo(modulo);
					      if(moduloGrabado.getId()==null){ 
					    	  mov.addObject( "messageerror","Error al grabar. Vuelva a intentarlo");
					      }
					       
			  }
			} catch (Exception e) { 
				mov.addObject(  "messageerror","Error generico. Si el problema persiste contacte a soporte"); 
				e.printStackTrace();
			}finally{ 
				return mov;
			}
	   }
	  
	 
	  @RequestMapping(method=RequestMethod.POST, value="modulo/eliminar") 
	   public  ModelAndView eliminar(@ModelAttribute("id")String id, ModelMap model, final RedirectAttributes redirectAttributes) {
		  ModelAndView mov=new ModelAndView(URL_PAGINA, "command",new EntModulo() );
		  try {  
			  moduloService.Eliminar(id);
		  	} catch (Exception e) { 
				mov.addObject(  "messageerror","Error generico. Si el problema persiste contacte a soporte");
				e.printStackTrace();
			}
	  		return mov; 
	   }
	 
}
